package main

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"github.com/dutchcoders/goftp"
	"io"
	"log"
	"os"
)

func main() {
	ftp, err := goftp.Connect("ftp.prod.zlbzb.cn:21")
	if err != nil {
		log.Fatalln("goftp.Connect error: %s", err)
	}
	defer ftp.Close()
	err = ftp.Login("pingan", "hQXmEbiuvQ#6VhQm")
	if err != nil {
		log.Fatalf("ftp.Login error: %s", err)
	}

	pwd, err := ftp.Pwd()
	if err != nil {
		log.Fatalf("ftp.Pwd error: %s", err)
	}
	fmt.Println(pwd)
	//files, err := ftp.List("/")
	//if err != nil {
	//	log.Fatalf("ftp.List error: %s",err)
	//}
	//fmt.Println(files)

	err = ftp.Walk("/", func(path string, info os.FileMode, err error) error {
		_, err = ftp.Retr(path, func(r io.Reader) error {
			var hasher = sha256.New()
			if _, err = io.Copy(hasher, r); err != nil {
				return err
			}

			hash := fmt.Sprintf("%s %x", path, hex.EncodeToString(hasher.Sum(nil)))
			fmt.Println(hash)

			return err
		})

		return nil
	})
}
